from users.models import Warning_mes,Info,News,Message
from courses.models import Course
from rewards.models import Reward
from groups.models import Topic

def warn(user):
	w_table=Warning_mes.objects.filter(to=user).filter(if_read=0).order_by('-time')[0:5]
	waningMes=[]
	for w in w_table:
		each=[]
		w_from_name=Info.objects.get(user_id=w.from_id).user_name
		each.append(w.from_id)				#0
		each.append(w_from_name)			#1
		each.append(w.aim_id)				#2
		if w.status==1 or w.status==7 or w.status==8 or w.status==11:
			aim_name=Reward.objects.get(id=w.aim_id).name
		elif w.status==2 or w.status==10:
			aim_name=Course.objects.get(id=w.aim_id).name
		elif w.status==3 or w.status==5 or w.status==9 or w.status==13:
			aim_name=News.objects.get(id=w.aim_id).content
		elif w.status==4 or w.status==6 or w.status==12:
			aim_name=Topic.objects.get(id=w.aim_id).name

		each.append(aim_name[0:10]+'...')				#3
		each.append(w.status)				#4
		each.append(w.review_id)				#5
		each.append(w.id)				#6
		waningMes.append(each)

		#if w.if_read==0:
		#	havent+=1
	m=Message.objects.filter(to=user).filter(status=1).order_by('-time')[0:5]
	mess=[]
	for k in m:
		each=[]
		name=Info.objects.get(user_id=k.from_id).user_name
		content=k.content
		each.append(name)		#0
		each.append(content)	#1
		each.append(k.from_id)	#2
		mess.append(each)
	msg=Message.objects.filter(to=user).filter(status=1).count()
	havent=0
	havent+=Warning_mes.objects.filter(to=user).filter(if_read=0).count()
	result=[]
	result.append(waningMes)
	result.append(int(havent)+int(msg))
	result.append(mess)
	return result